package com.spar.openprojects.client.composite;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.spar.openprojects.client.RemoteDataService;
import com.spar.openprojects.client.RemoteDataServiceAsync;
import com.spar.openprojects.shared.Member;
import com.spar.openprojects.shared.Order;
import com.spar.openprojects.shared.OrderItem;
import com.spar.openprojects.shared.Project;

public class OrdersComposite extends Composite {
	private DecoratorPanel decoratorPanel = new DecoratorPanel();
	private Project project;
	private List<Order> orders;
	private RemoteDataServiceAsync asyncService = GWT
			.create(RemoteDataService.class);
	private Member user = new Member();
	public DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
	public NumberFormat nf = NumberFormat.getFormat("0");
	private VerticalPanel verticalPanel = new VerticalPanel();
	private HorizontalPanel horizontalPanel = new HorizontalPanel();
	private Button buttonNewOrder = new Button("New Order");
	private CellTable<Order> cellTableProjects = new CellTable<Order>();

	private TextColumn<Order> colOrderId = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return order.getId().toString();
		}
	};
	private Column<Order, Anchor> colOfferColumn = new Column<Order, Anchor>(
			new AnchorCell()) {
		@Override
		public Anchor getValue(Order order) {
			Anchor link = new Anchor();
			link.setHref(order.getOffer().getLink());
			link.setText(order.getOffer().getNumber());
			return link;
		}
	};
	private Column<Order, Anchor> colOrderColumn = new Column<Order, Anchor>(
			new AnchorCell()) {
		@Override
		public Anchor getValue(Order order) {
			Anchor link = new Anchor();
			link.setHref(order.getOrder().getLink());
			link.setText(order.getOrder().getNumber());
			return link;
		}
	};
	private Column<Order, Anchor> colBillColumn = new Column<Order, Anchor>(
			new AnchorCell()) {
		@Override
		public Anchor getValue(Order order) {
			Anchor link = new Anchor();
			if (order.getBills().size() > 0) {
				link.setHref(order.getBills().get(0).getLink());
				link.setText(order.getBills().get(0).getNumber());
			}
			return link;
		}
	};
	private TextColumn<Order> colOrderBillAmount = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return nf.format(order.getBillAmount());
		}
	};
	private TextColumn<Order> colAmountPaid = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			if (order.getOrder().getAmount().equals(0))
				return "0";
			return nf.format(order.getBillAmount() * 100
					/ order.getOrder().getAmount());
		}
	};
	private TextColumn<Order> colOrderOfferNumber = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return order.getOffer().getNumber();
		}
	};
	private TextColumn<Order> colOrderOrderAmount = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return nf.format(order.getOrder().getAmount());
		}
	};
	private TextColumn<Order> colOrderOrderNumber = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return order.getOrder().getNumber();
		}
	};
	private TextColumn<Order> colOrderCreated = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return dtf.format(order.getCreated());
		}
	};
	private TextColumn<Order> colOrderBusinessPartner = new TextColumn<Order>() {
		@Override
		public String getValue(Order order) {
			return order.getBusinessPartner().getName();
		}
	};
	private final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
	private final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
	private final Label labelProjectName = new Label("");

	public OrdersComposite() {
		initWidget(decoratorPanel);
		decoratorPanel.setWidth("1000px");
		initPanel();

	}

	public void loadOrders(final Project project, Member user) {
		this.project = project;
		this.user = user;
		asyncService.getOrders(project, user, new AsyncCallback<List<Order>>() {

			@Override
			public void onSuccess(List<Order> result) {
				orders = result;
				if (orders != null) {
					cellTableProjects.setRowCount(0);
					cellTableProjects.setRowData(0, orders);
				}
				labelProjectName.setText(project.getDescription());
			}

			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(
						new Label(caught.getMessage()));
			}
		});

	}

	private void initPanel() {
		horizontalPanel.setSpacing(10);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("1000px");
		verticalPanel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("300px");

		horizontalPanel_1.add(labelProjectName);
		horizontalPanel_2
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setWidth("700px");
		horizontalPanel_2.add(buttonNewOrder);

		buttonNewOrder.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				createNewOrder(user);
			}
		});
		cellTableProjects.setPageSize(50);
		cellTableProjects.setWidth("1000px");

		verticalPanel.add(cellTableProjects);
		decoratorPanel.setWidget(verticalPanel);

		// cellTableProjects.addColumn(colOrderId, "Id");
		// cellTableProjects.addColumn(colOrderOfferNumber, "Offer Number");
		cellTableProjects.addColumn(colOfferColumn, "Offer Number");
		cellTableProjects.addColumn(colOrderColumn, "Order Number");
		cellTableProjects.addColumn(colBillColumn, "Bill Number");
		cellTableProjects.addColumn(colOrderOrderAmount, "Order Amount");
		cellTableProjects.addColumn(colOrderBillAmount, "Bill Amount");
		cellTableProjects.addColumn(colAmountPaid, "Amount Paid %");
		// cellTableProjects.addColumn(colOrderOrderNumber, "Order Number");
		cellTableProjects.addColumn(colOrderBusinessPartner, "Partner");
		cellTableProjects.addColumn(colOrderCreated, "Created");

		final SingleSelectionModel<Order> selectionModel = new SingleSelectionModel<Order>();
		cellTableProjects.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						Order order = (Order) selectionModel
								.getSelectedObject();
						order.setUpdateMember(user);
						selectOrder(order, user);
					}
				});

	}

	private void createNewOrder(Member user) {
		Order order = new Order();
		order.setId(0);
		order.setProjectId(project.getId());
		order.getOrder().setNumber("New order");
		order.getBills().add(new OrderItem());
		Member updateMember = new Member();
		updateMember.setNumber(user.getNumber());
		order.setUpdateMember(updateMember);
		Member createMember = new Member();
		createMember.setNumber(user.getNumber());
		order.setCreateMember(createMember);
		orders.add(order);
		cellTableProjects.setRowData(0, orders);
		selectOrder(order, user);
	}

	private void selectOrder(Order order, Member user) {
		RootPanel.get("infoContainer").clear();
		Iterator<Widget> iterator = RootPanel.get("openprojectsContainer")
				.iterator();
		Widget widget;
		while (iterator.hasNext()) {
			widget = iterator.next();
			if (widget instanceof OrderComposite) {
				RootPanel.get("openprojectsContainer").remove(widget);
			}
		}
		RootPanel.get("openprojectsContainer").add(
				new OrderComposite(order, project, user));

	}
}
